


//
//  MBTextViewCell.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/7.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBTextViewCell.h"

@interface MBTextViewCell() <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *redLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;

@end

@implementation MBTextViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.numLabel.text= @"0/800";
    self.textView.delegate = self;
    [self addRetunKeyAtToolBar];
    // Initialization code
}
-(void)setCMModel:(MBCommonCellModel *)CMModel{
    [super setCMModel:CMModel];
    
    //1. *
    if (CMModel.hasX) {
        self.redLabel.hidden = NO;
    }else{
        self.redLabel.hidden = YES;
    }
    
    //2.标题
    self.nameLabel.text = CMModel.title;
    
    //3.textview 的占位文字；
    
    
    
    
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    
    [textView resignFirstResponder];
    self.CMModel.sourceTitle = textView.text;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    return  YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSLog(@"%@---%@",textView.text,text);
    if (textView.text.length > 800) {
        return  NO;
    }
    return  YES;
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    
}
-(void)textViewDidChange:(UITextView *)textView{
    
    NSUInteger num =  textView.text.length;
   
    
    self.numLabel.text = [NSString stringWithFormat:@"%zd/800",num];
}


-(void)addRetunKeyAtToolBar{
    UIToolbar *bar  = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, SCREEN_WIDTH- 40, 80, 40);
    [btn addTarget:self action:@selector(downKeyboard) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(downKeyboard)];
    
    UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithCustomView:btn];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    bar.items = @[item1,item0];
    self.textView.inputAccessoryView = bar;
    
}
-(void)downKeyboard{
    [self.textView resignFirstResponder];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
